//
//  HomeMenuCell.m
//  pmmSS
//
//  Created by Mac book on 2017/8/29.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//
#import "HomeMenuCell.h"




@interface _HomeMenu : UIView

@property(nonatomic,strong)UIImageView * icon;

@property(nonatomic,strong)UILabel * textLabel;

-(void)addTarge:(id)target select:(SEL)select;

-(NSString*)currentTitle;


+(instancetype)menuWithIconName:(NSString*)iconName title:(NSString*)title;

@end

@implementation _HomeMenu

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView * icon = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.icon = icon;
        [self addSubview:icon];
        icon.layer.cornerRadius = 3;
        icon.layer.masksToBounds = YES;

        UILabel * textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:textLabel];
        self.textLabel = textLabel;
        self.textLabel.textAlignment = NSTextAlignmentCenter;
        self.textLabel.textColor = [UIColor blackColor];
        self.textLabel.font = [UIFont systemFontOfSize:14];

        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.top.equalTo(self.mas_top).offset(10);
            make.width.height.equalTo(self.mas_width).multipliedBy(0.5);
        }];

        [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.top.equalTo(icon.mas_bottom).offset(2);
            make.bottom.equalTo(self.mas_bottom);
            make.width.equalTo(self.mas_width);
        }];
    }
    return self;
}

-(void)addTarge:(id)target select:(SEL)select{
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:target action:select]];
}

-(NSString *)currentTitle{
    return self.textLabel.text;
}


+(instancetype)menuWithIconName:(NSString*)iconName title:(NSString*)title{
    _HomeMenu * item = [[self alloc] initWithFrame:CGRectZero];
    item.icon.image = [UIImage imageNamed:iconName];
    item.textLabel.text = title;
    return item;
}

@end


@interface HomeMenuCell()<UIScrollViewDelegate>


///容器view
@property(nonatomic,strong)UIScrollView * scrollView;

/** 页码控制器 */
@property (strong, nonatomic) UIPageControl *pageControl;

/** 个数 */
@property (assign, nonatomic) NSInteger pageCounts;


@end
@implementation HomeMenuCell


- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self iniSubViews];
    }
    return self;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self iniSubViews];
    }
    return self;
}


-(void)iniSubViews{

    NSLog(@"HEIGHT=====%f",self.contentView.frame.size.height);

    NSLog(@"WIDTH=====%f",self.contentView.frame.size.height);
    
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    scrollView.pagingEnabled = YES;
    [self.contentView addSubview:scrollView];

    self.scrollView = scrollView;
    self.scrollView.delegate = self;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.height.equalTo(self.contentView.mas_height);
        make.width.equalTo(self.contentView.mas_width);
    }];



}

-(void)setIcons:(NSArray<NSString *> *)icons andTitles:(NSArray<NSString *> *)titles{

    
    if (icons.count!=titles.count) {
        return;
    }

    NSInteger coun = icons.count;
    if (coun/10>0) {
        coun = coun/10+1;
    }else{
        coun = 1;
    }
    __block _HomeMenu * lastMenu = nil;
    __block NSInteger  lastPage = 0;
    __block NSInteger  lastRow = 0;
    for (int i = 0 ; i<icons.count; i++) {
        NSString * icon = icons[i];
        NSString * title = titles[i];
        //-----------************---------
        _HomeMenu * menu = [_HomeMenu menuWithIconName:icon title:title];
        [menu addTarge:self select:@selector(menuClick:)];
        [self.scrollView addSubview:menu];

        //-----------************---------
        
        _pageCounts = i/10;
        NSInteger row = (i%10)/5 == 0;
        [menu mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(self.contentView.mas_height).multipliedBy(0.5);
            make.width.equalTo(self.contentView.mas_width).multipliedBy(0.2);
            if (lastMenu) {
                if (_pageCounts!=lastPage) {//不同页
                    make.left.equalTo(lastMenu.mas_right).offset(0);
                    make.top.equalTo(self.scrollView.mas_top).offset(0);
                }else{//同一页
                    if (row == lastRow) {//同一行
                        make.left.equalTo(lastMenu.mas_right).offset(0);
                        make.top.equalTo(lastMenu.mas_top);
                    }else{//不同行
                        make.left.equalTo(self.scrollView.mas_left).offset(0);
                        make.top.equalTo(lastMenu.mas_bottom).offset(0);
                    }
                }
            }else{
                make.left.equalTo(self.scrollView.mas_left).offset(0);
                make.top.equalTo(self.scrollView.mas_top).offset(0);
            }
        }];
        lastRow = row;
        lastPage = _pageCounts;
        lastMenu = menu;
    }

    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectZero];

    self.pageControl.numberOfPages = coun;
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithRed:(164 / 255.0f) green:(164 / 255.0f) blue:(164 / 255.0f) alpha:1];
    self.pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:(73 / 255.0f) green:(73 / 255.0f) blue:(73 / 255.0f) alpha:1];
    [self.contentView addSubview:self.pageControl];

    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.bottom.mas_equalTo(self.contentView).offset(autoScaleH(-2));
        make.height.mas_equalTo(autoScaleH(10));
        make.width.equalTo(self.contentView.mas_width);

    }];
}

-(void)layoutSubviews{
    NSInteger coun = self.scrollView.subviews.count-2;
    if (coun/10>=1&&coun>10) {
        coun = coun/10+1;
    }else{
        coun = 0;
    }
    self.scrollView.contentSize = CGSizeMake(self.contentView.frame.size.width*coun, 0);
}


-(void)menuClick:(id)sender{
    _HomeMenu * menu =(_HomeMenu *)[sender view];

    if (self.delegate && [self.delegate respondsToSelector:@selector(HomeMenuCellDidClick:)]) {

        [self.delegate HomeMenuCellDidClick:[menu currentTitle]];
    }
}
// scrollView滚动时执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = self.scrollView.contentOffset;
    CGFloat offsetX = offset.x;
    CGFloat width = self.scrollView.frame.size.width;
    int pageNum = (offsetX + .5f *  width) / width;

    self.pageControl.currentPage = pageNum;
}

@end
